<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/08/03 10:35:07 $)
// $RCSfile: staff_ticketactions.php,v $ : $Revision: 1.32 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

define("CUSTOMFIELD_STAFFUSERTICKET",9);

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("tickets_auditlogs");

require_once ("./modules/tickets/functions_ticketmain.php");
require_once ("./modules/tickets/functions_ticketcore.php");
require_once ("./modules/tickets/functions_attachments.php");
require_once ("./modules/tickets/functions_followup.php");

/**
* ###############################################
* REPLY
* ###############################################
*/
if ($_POST["action"] == "reply")
{
	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_tdepartmentid = $_ticketobj->ticket["departmentid"];
	$_tticketstatusid = $_ticketobj->ticket["ticketstatusid"];

	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];

	if ($_gridset["replynav"] ==2)
	{
		$nextticketid = $_ticketobj->getNextTicketID();
	} else {
		unset($nextticketid);
	}

	// Do we need to add a note to this ticket?
	if (trim($_POST["notecontents"]) != "")
	{
		$_ticketobj->addTicketNote($_POST["notetype"], "0", $_POST["notecontents"]);
	}

	$ticketlistmove = false;
	if ($_POST["tt_staffid"] != $_ticketobj->ticket["ownerstaffid"])
	{
		$_ticketobj->assignTicket($_POST["tt_staffid"]);
	}
	if ($_POST["tt_ticketstatusid"] != $_ticketobj->ticket["ticketstatusid"])
	{
		$_ticketobj->changeStatus($_POST["tt_ticketstatusid"]);
	}
	if ($_POST["tt_priorityid"] != $_ticketobj->ticket["priorityid"])
	{
		$_ticketobj->changePriority($_POST["tt_priorityid"]);
	}
	if ($_POST["tt_departmentid"] != $_ticketobj->ticket["departmentid"])
	{
		// Get the assigned departments
		$_ticketobj->moveTicket($_POST["tt_departmentid"]);
		$_assigns = getAssignedDepartments(MODULE_TICKETS);
		if (!in_array($_POST["tt_departmentid"], $_assigns))
		{
			// Move to Ticket List
			$ticketlistmove = true;
		}
	}

	if (!$_POST["opt_sendemail"])
	{
		$_dontsendemail = true;
	} else {
		$_dontsendemail = false;
	}

	// Do we need to save it as a draft?
	if ($_POST["saveasdraft"] == "2")
	{
		$_ticketobj->saveDraft($_POST["replycontents"]);
	} else if (trim($_POST["replycontents"]) != "") {
		// Add a reply
		$ticketpostid = $_ticketobj->createStaffReply($_SWIFT["staff"]["staffid"], $_SWIFT["staff"]["fullname"], $_SWIFT["staff"]["email"], "", $_POST["replycontents"], false, $_POST["opt_emailqueueid"], "", $_POST["opt_cc"], $_POST["ccaddrecpients"], $_POST["opt_bcc"], $_POST["bccaddrecpients"], $_POST["opt_downloads"], false, $_dontsendemail);
	} else {
		// Redirect back to ticket with error
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"])."&replyerror=1".iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	}

	if ($_POST["followupvalue"])
	{
		$_followupresult = parseFollowUp($_ticketobj);
	}

	// Do we need to update the due timeline?
	if (!empty($_POST["opt_due"]))
	{
		$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
	}

	if (!empty($_POST["opt_timeworked"]) && is_numeric($_POST["opt_timeworked"]))
	{
		$_ticketobj->addBillingEntry(DATENOW, $_POST["opt_timeworked"], intval($_POST["opt_timebillable"]), $_SWIFT["staff"]["staffid"], "");
	}

	if (!empty($_POST["opt_addpred"]) && !empty($ticketpostid))
	{
		// We override all pages and simply redirect user to insert predefined page..
		header("location: index.php?_m=tickets&_a=insertpredreply&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&ticketpostid=".intval($ticketpostid)."&opt_addkb=".intval($_POST["opt_addkb"])."&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid));
		exit;
	} else if (!empty($_POST["opt_addkb"]) && !empty($ticketpostid)) {
		// We override all pages and simply redirect user to insert predefined page..
		header("location: index.php?_m=knowledgebase&_a=insertquestion&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&ticketpostid=".intval($ticketpostid)."&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid));
		exit;
	}
	

	if (!$ticketlistmove && $_gridset["replynav"] == 1)
	{
		// Take back to ticket
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	} else if (!empty($_POST["ticketlabelid"])) {
		header("location: index.php?_m=tickets&_a=manage&ticketlabelid=".intval($_POST["ticketlabelid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	} else if ($nextticketid) {
		// Take to next active ticket in department
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($nextticketid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	}
	
	header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
	exit;




/**
* ###############################################
* FORWARD
* ###############################################
*/
} else if ($_POST["action"] == "forward" && $_SWIFT["staff"]["tforward"] != "0") {
	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_tdepartmentid = $_ticketobj->ticket["departmentid"];
	$_tticketstatusid = $_ticketobj->ticket["ticketstatusid"];

	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];

	// Do we need to add a note to this ticket?
	if (trim($_POST["notecontents"]) != "")
	{
		$_ticketobj->addTicketNote($_POST["notetype"], "0", $_POST["notecontents"]);
	}

	$ticketlistmove = false;
	if ($_POST["tt_staffid"] != $_ticketobj->ticket["ownerstaffid"])
	{
		$_ticketobj->assignTicket($_POST["tt_staffid"]);
	}
	if ($_POST["tt_ticketstatusid"] != $_ticketobj->ticket["ticketstatusid"])
	{
		$_ticketobj->changeStatus($_POST["tt_ticketstatusid"]);
	}
	if ($_POST["tt_priorityid"] != $_ticketobj->ticket["priorityid"])
	{
		$_ticketobj->changePriority($_POST["tt_priorityid"]);
	}
	if ($_POST["tt_departmentid"] != $_ticketobj->ticket["departmentid"])
	{
		// Get the assigned departments
		$_ticketobj->moveTicket($_POST["tt_departmentid"]);
		$_assigns = getAssignedDepartments(MODULE_TICKETS);
		if (!in_array($_POST["tt_departmentid"], $_assigns))
		{
			// Move to Ticket List
			$ticketlistmove = true;
		}
	}

	// Do we need to save it as a draft?
	if ($_POST["saveasdraft"] == "2")
	{
		$_ticketobj->saveDraft($_POST["replycontents"]);
	} else if (trim($_POST["replycontents"]) != "") {
		// Forward the Ticket...
		$ticketpostid = $_ticketobj->forwardTicket($_SWIFT["staff"]["staffid"], $_SWIFT["staff"]["fullname"], $_SWIFT["staff"]["email"], $_POST["opt_to"], $_ticketobj->ticket["subject"], $_POST["replycontents"], $_POST["opt_emailqueueid"], $_POST["faddrecipients"], $_POST["opt_downloads"], $_POST["attachments"]);
	} else {
		// Redirect back to ticket with error
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"])."&replyerror=1");
		exit;
	}

	// Do we need to update the due timeline?
	if (!empty($_POST["opt_due"]))
	{
		$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
	}

	//Mahesh: Timeworked track....
	if (!empty($_POST["opt_timeworked"]) && is_numeric($_POST["opt_timeworked"]))
	{
		$_ticketobj->addBillingEntry(DATENOW, $_POST["opt_timeworked"], $_POST["opt_timeworked"], $_SWIFT["staff"]["staffid"], "");
	}
	//--------------------

	if ($_POST["followupvalue"])
	{
		$_followupresult = parseFollowUp($_ticketobj);
	}

	if (!empty($_POST["ticketlabelid"])) {
		header("location: index.php?_m=tickets&_a=manage&ticketlabelid=".intval($_POST["ticketlabelid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	} else {
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	}


/**
* ###############################################
* RELEASE TAB
* ###############################################
*/
} else if ($_POST["action"] == "release") {
	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_tdepartmentid = $_ticketobj->ticket["departmentid"];
	$_tticketstatusid = $_ticketobj->ticket["ticketstatusid"];

	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];

	if ($_gridset["replynav"] ==2)
	{
		$nextticketid = $_ticketobj->getNextTicketID();
	} else {
		unset($nextticketid);
	}

	// Do we need to add a note to this ticket?
	if (trim($_POST["notecontents"]) != "")
	{
		$_ticketobj->addTicketNote($_POST["notetype"], "0", $_POST["notecontents"]);
	}

	$ticketlistmove = false;
	if ($_POST["tt_staffid"] != $_ticketobj->ticket["ownerstaffid"])
	{
		$_ticketobj->assignTicket($_POST["tt_staffid"]);
	}
	if ($_POST["tt_ticketstatusid"] != $_ticketobj->ticket["ticketstatusid"])
	{
		$_ticketobj->changeStatus($_POST["tt_ticketstatusid"]);
	}
	if ($_POST["tt_priorityid"] != $_ticketobj->ticket["priorityid"])
	{
		$_ticketobj->changePriority($_POST["tt_priorityid"]);
	}
	if ($_POST["tt_departmentid"] != $_ticketobj->ticket["departmentid"])
	{
		// Get the assigned departments
		$_ticketobj->moveTicket($_POST["tt_departmentid"]);
		$_assigns = getAssignedDepartments(MODULE_TICKETS);
		if (!in_array($_POST["tt_departmentid"], $_assigns))
		{
			// Move to Ticket List
			$ticketlistmove = true;
		}
	}

	// Do we need to update the due timeline?
	if (!empty($_POST["opt_due"]))
	{
		$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
	}
	
	//Mahesh: Timeworked track....
	if (!empty($_POST["opt_timeworked"]) && is_numeric($_POST["opt_timeworked"]))
	{
		$_ticketobj->addBillingEntry(DATENOW, $_POST["opt_timeworked"], $_POST["opt_timeworked"], $_SWIFT["staff"]["staffid"], "");
	}
	//--------------------

	if ($_POST["followupvalue"])
	{
		$_followupresult = parseFollowUp($_ticketobj);
	}

	if (!empty($_POST["ticketlabelid"])) {
		header("location: index.php?_m=tickets&_a=manage&ticketlabelid=".intval($_POST["ticketlabelid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	} else if ($nextticketid) {
		// Take to next active ticket in department
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($nextticketid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	}
	
	header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
	exit;




/**
* ###############################################
* FOLLOW-UP TAB
* ###############################################
*/
} else if ($_POST["action"] == "followup") {
	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_tdepartmentid = $_ticketobj->ticket["departmentid"];
	$_tticketstatusid = $_ticketobj->ticket["ticketstatusid"];

	$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];

	if ($_gridset["replynav"] ==2)
	{
		$nextticketid = $_ticketobj->getNextTicketID();
	} else {
		unset($nextticketid);
	}

	$_followupresult = parseFollowUp($_ticketobj);

	if (!empty($_POST["ticketlabelid"])) {
		header("location: index.php?_m=tickets&_a=manage&ticketlabelid=".intval($_POST["ticketlabelid"]).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	} else if ($nextticketid) {
		// Take to next active ticket in department
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($nextticketid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
		exit;
	}
	
	header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid).iif($_POST["dofollowup"], "&followupresult=".intval($_followupresult)));
	exit;




/**
* ###############################################
* EDIT
* ###############################################
*/
} else if ($_POST["action"] == "edit" && !empty($_POST["ticketid"])) {
	require_once ("./includes/functions_customfields.php");

	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if (!empty($_POST["recipient"]) && isValidEmail($_POST["recipient"]))
	{
		if ($_POST["recipienttype"] == "thirdparty")
		{
			$recipienttype = RECIPIENT_THIRDPARTY;
		} else if ($_POST["recipienttype"] == "ccuser") {
			$recipienttype = RECIPIENT_CCUSER;
		} else if ($_POST["recipienttype"] == "bccuser") {
			$recipienttype = RECIPIENT_BCCUSER;
		}

		if ($recipienttype)
		{
			$_ticketobj->addRecipients(array($_POST["recipient"]), $recipienttype);
		}
	} else if (checkCustomFieldContent(CUSTOMFIELD_USERTICKET, false, $_ticketobj->ticket["ticketid"]) && checkCustomFieldContent(CUSTOMFIELD_STAFFTICKET, false, $_ticketobj->ticket["ticketid"]) && checkCustomFieldContent(CUSTOMFIELD_STAFFUSERTICKET, false, $_ticketobj->ticket["ticketid"])) {
		updateCustomFields(CUSTOMFIELD_USERTICKET, $_ticketobj->ticket["ticketid"], false, $_ticketobj->ticket["ticketid"]);
		updateCustomFields(CUSTOMFIELD_STAFFTICKET, $_ticketobj->ticket["ticketid"], false, $_ticketobj->ticket["ticketid"]);
		updateCustomFields(CUSTOMFIELD_STAFFUSERTICKET, $_ticketobj->ticket["ticketid"], false, $_ticketobj->ticket["ticketid"]);
	}

	if (!empty($_POST["linkcustomfieldgroupid"]))
	{
		// Fetch the group first
		$_customfieldgroup = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."customfieldgroups` WHERE `customfieldgroupid` = '". intval($_POST["linkcustomfieldgroupid"]) ."';");
		if (!empty($_customfieldgroup["customfieldgroupid"]))
		{
			// Link to custom field group

			addCustomFieldLink($_customfieldgroup["grouptype"], $_ticketobj->ticket["ticketid"], $_POST["linkcustomfieldgroupid"]);
		}
	}

	$redirticketid = intval($_ticketobj->ticket["ticketid"]);

	if (!empty($_POST["mergeticketid"]) && $_SWIFT["staff"]["tmerge"] != "0")
	{
		// Try to load this ticket
		if (is_numeric($_POST["mergeticketid"]))
		{
			$_mergeticketid = intval($_POST["mergeticketid"]);
		} else {
			$_mergeticketid = getTicketIDFromMask($_POST["mergeticketid"]);
		}

		$mergeresult = mergeTickets($_mergeticketid, array($_ticketobj->ticket["ticketid"]));
		if ($mergeresult)
		{
			$redirticketid = $_mergeticketid;
		}
	}

	$_ticketobj->updateProperties($_POST["subject"], $_POST["email"], $_POST["fullname"]);

	header("location: index.php?_m=tickets&_a=viewticket&ticketid=".$redirticketid);
	exit;











/**
* ###############################################
* FETCH ATTACHMENT
* ###############################################
*/
} else if ($_GET["action"] == "attachment") {
	$_ticketobj =& new TicketMain($_GET["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if (!empty($_GET["attachmentid"]))
	{
		require_once ("./modules/tickets/functions_attachments.php");
		$_attach = new TicketAttachments();
		$_attach->dispatchAttachment($_GET["attachmentid"], $_ticketobj->ticket["ticketid"]);
		exit;
	}



/**
* ###############################################
* BILLLING TAB
* ###############################################
*/
} else if ($_POST["action"] == "billing" && !empty($_POST["ticketid"]) && $_SWIFT["staff"]["tbilling"] != "0") {
	require_once ("./includes/functions_customfields.php");

	$_ticketobj =& new TicketMain($_POST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if (!empty($_POST["tworked"]) && (!empty($_POST["tbillable"]) || $_POST["tbillable"] == '0') && checkCustomFieldContent(CUSTOMFIELD_TIMETRACK))
	{
		require_once ("./includes/functions_date.php");

		$timetrackid = $_ticketobj->addBillingEntry(returnParsedTime($_POST["billdate"]), $_POST["tworked"], $_POST["tbillable"], $_POST["workerstaffid"], $_POST["billsummary"]);
		if ($timetrackid)
		{
			updateCustomFields(CUSTOMFIELD_TIMETRACK, $timetrackid, false, false);
		}
	}

	header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]));
	exit;





/**
* ###############################################
* FLAG TICKET
* ###############################################
*/
} else if ($_GET["action"] == "flagticket") {
	$flagarray = array("purple" => FLAG_PURPLE, "red" => FLAG_RED, "orange" => FLAG_ORANGE, "yellow" => FLAG_YELLOW, "blue" => FLAG_BLUE, "green" => FLAG_GREEN);
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	$flagresult = false;
	if (isset($flagarray[$_GET["flag"]]) && $_ticketobj->ticket)
	{
		if ($_ticketobj->flagTicket($flagarray[$_GET["flag"]]))
		{
			$flagresult = true;
		}
	} else {
		$_ticketobj->clearFlag();
	}

	printRedirect(sprintf($_SWIFT["language"]["flagconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&flagconfirm=1");


/**
* ###############################################
* PREVIOUS/NEXT TICKETS
* ###############################################
*/
} else if ($_GET["action"] == "jump") {
	$_ticketobj =& new TicketMain($_GET["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	unset($_ticketid);
	if ($_GET["t"] == "previous")
	{
		$_ticketid = $_ticketobj->getPreviousTicketID();
	} else if ($_GET["t"] == "next") {
		$_ticketid = $_ticketobj->getNextTicketID();
	}

	// Couple of things, 1) If we have the ticket id then great, just redirect to it 2) If we have no ticket id, we need to redirect to ticket listing 3) Blah Blah Blah
	if (!empty($_ticketid))
	{
		header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketid));
	} else {
		header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_ticketobj->ticket["departmentid"])."&ticketstatusid=".intval($_ticketobj->ticket["ticketstatusid"]));
	}
	exit;





/**
* ###############################################
* EXPORT
* ###############################################
*/
} else if ($_GET["action"] == "export") {
	if ($_GET["type"] == "xml")
	{
		$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);
		if ($_ticketobj->ticket && $_SWIFT["staff"]["texport"] != "0")
		{
			$_ticketobj->exportXML();
		} else {
			echo $_SWIFT["language"]["errorexport"];
			exit;
		}
	} else if ($_GET["type"] == "pdf") {
		$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);
		if ($_ticketobj->ticket && $_SWIFT["staff"]["texport"] != "0")
		{
			$_ticketobj->exportPDF();
		} else {
			echo $_SWIFT["language"]["errorexport"];
			exit;
		}
	}




/**
* ###############################################
* MARK AS DUE
* ###############################################
*/
} else if ($_GET["action"] == "markdue") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errordue"];
		exit;
	}

	if ($_GET["type"] == "due")
	{
		$_ticketobj->markAsDue();
	} else if ($_GET["type"] == "due24") {
		$_ticketobj->markAsDue((DATENOW+86400));
	}

	printRedirect(sprintf($_SWIFT["language"]["dueconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&dueconfirm=1");





/**
* ###############################################
* TICKET OPTIONS
* ###############################################
*/
} else if ($_GET["action"] == "options") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["erroroption"];
		exit;
	}


	if ($_GET["type"] == "print")
	{
		$template->assign("documenttitle", $_ticketobj->ticket["fticketid"]);
		echo $template->displayTemplate("header.tpl");
		$_ticketobj->printTicket(iif(empty($_GET["ticketpostid"]), false, $_GET["ticketpostid"]));
	} else if ($_GET["type"] == "ban") {
		if ($_SWIFT["staff"]["tbanticket"] != "0")
		{
		banTickets(array($_ticketobj->ticket["ticketid"]), true);
		printRedirect(sprintf($_SWIFT["language"]["banconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]) ."&banconfirm=".urlencode($_ticketobj->ticket["email"]));
		} else {
			printRedirect(sprintf($_SWIFT["language"]["bandconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]));
		}
	} else if ($_GET["type"] == "bandelete") {
		if ($_SWIFT["staff"]["tbandelete"] != "0")
		{
			banTickets(array($_ticketobj->ticket["ticketid"]), true);
			deleteTickets(array($_ticketobj->ticket["ticketid"]), true);
			printRedirect(sprintf($_SWIFT["language"]["bandconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]) ."&bandconfirm=".urlencode($_ticketobj->ticket["email"]));
		} else {
			printRedirect(sprintf($_SWIFT["language"]["bandconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]));
		}
	} else if ($_GET["type"] == "delete") {
		if ($_SWIFT["staff"]["tdeleteticket"] != "0")
		{
			deleteTickets(array($_ticketobj->ticket["ticketid"]), true);

			printRedirect(sprintf($_SWIFT["language"]["delconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]) ."&delconfirm=".urlencode($_ticketobj->ticket["fticketid"]));
		} else {
			printRedirect(sprintf($_SWIFT["language"]["delconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]));
		}
	} else if ($_GET["type"] == "deletepost") {
		if ($_SWIFT["staff"]["tdeletepost"] != "0")
		{
			$delresult = $_ticketobj->deleteTicketPost($_GET["ticketpostid"]);
			printRedirect(sprintf($_SWIFT["language"]["delpostconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&delpresult=".intval($delresult));
		} else {
			printRedirect(sprintf($_SWIFT["language"]["delpostconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]));
		}
	} else if ($_GET["type"] == "headers") {
		if ($module->isRegistered(MODULE_PARSER))
		{
			$template->loadLanguageSection("emailparser");
			require_once ("./modules/parser/functions_parser.php");
			viewParserLog(false, $_GET["ticketpostid"]);
		}
	}
	



/**
* ###############################################
* ADD NOTE
* ###############################################
*/
} else if ($_POST["action"] == "addnote") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if (trim($_POST["notecontents"]) != "")
	{
		$_ticketobj->addTicketNote($_POST["notetype"], $_POST["forstaffid"], $_POST["notecontents"]);
	}

	printRedirect(sprintf($_SWIFT["language"]["noteconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&noteconfirm=1");
	exit;




/**
* ###############################################
* LINK SLA PLAN
* ###############################################
*/
} else if ($_GET["action"] == "linksla") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}
	$_ticket = &$_ticketobj->ticket;

	if ($_GET["slatype"] == "ticket" && !empty($_GET["slaplanid"]))
	{
		$_ticketobj->resetSLATimeline($_GET["slaplanid"]);
//		$_ticketobj->updatesql[] = "`ticketslaplanid` = '". intval($_GET["slaplanid"]) ."'";
//		$_ticketobj->updatesql[] = "`slaplanid` = '". intval($_GET["slaplanid"]) ."'";

		printRedirect(sprintf($_SWIFT["language"]["slatconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&slatconfirm=1");


	} else if ($_GET["slatype"] == "user" && !empty($_GET["slaplanid"]) && !empty($_ticket["userid"])) {
		require_once ("./includes/functions_users.php");
		updateUserSLAPlan($_ticketobj->ticket["userid"], intval($_GET["slaplanid"]));

		// Now get all tickets which fall under same status and set their SLA Plans
		$_ticketobjlist = $_ticketidlist = $_departmentidlist = array();
		// First build list of available departments
		foreach ($_SWIFT["departmentcache"] as $key=>$val)
		{
			if ($val["departmentmodule"] == MODULE_TICKETS)
			{
				$_departmentidlist[] = $val["departmentid"];
			}
		}

		// Now fetch available tickets
		$dbCore->query("SELECT `ticketid` FROM `". TABLE_PREFIX ."tickets` WHERE `departmentid` IN (". buildIN($_departmentidlist) .") AND `ticketstatusid` = '". intval($_ticket["ticketstatusid"]) ."' AND `userid` = '". intval($_ticket["userid"]) ."';");
		while ($dbCore->nextRecord())
		{
			$_ticketidlist[] = $dbCore->Record["ticketid"];
		}

		// Now load the corresponding objects and set their SLA
		$index = 0;
		foreach ($_ticketidlist as $key=>$val)
		{
			if ($val != $_ticket["ticketid"])
			{
				$_ticketobjlist[$index] =& new TicketMain($val, false, LOG_STAFF);
				$_pointerticket = &$_ticketobjlist[$index];
				$_pointerticket->resetSLATimeline($_GET["slaplanid"]);
				$index++;
			}
		}

		// Reset SLA Of the current ticket
		$_ticketobj->resetSLATimeline($_GET["slaplanid"]);

		printRedirect(sprintf($_SWIFT["language"]["slauconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&slauconfirm=1");
	}

	exit;



/**
* ###############################################
* MASS REPLY
* ###############################################
*/
} else if ($_POST["action"] == "massreply") {
	$ticketobjects = array();
	$index = 0;
	foreach ($_POST["ticketids"] as $key=>$val)
	{
		$ticketobjects[$index] =& new TicketMain($val, true);
		$_ticketobj = &$ticketobjects[$index];
		$index++;
		if (!$_ticketobj->ticket)
		{
			echo $_SWIFT["language"]["errorticketaccess"];
			exit;
		}

		// Do we need to add a note to this ticket?
		if (trim($_POST["notecontents"]) != "")
		{
			$_ticketobj->addTicketNote($_POST["notetype"], "0", $_POST["notecontents"]);
		}

		// Add a reply
		$ticketpostid = $_ticketobj->createStaffReply($_SWIFT["staff"]["staffid"], $_SWIFT["staff"]["fullname"], $_SWIFT["staff"]["email"], "", $_POST["replycontents"], false, $_POST["opt_emailqueueid"], "", $_POST["opt_cc"], $_POST["ccaddrecpients"], $_POST["opt_bcc"], $_POST["bccaddrecpients"], $_POST["opt_downloads"]);

		// Do we need to update the due timeline?
		if (!empty($_POST["opt_due"]))
		{
			$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
		}

		if (!empty($_POST["opt_timeworked"]) && is_numeric($_POST["opt_timeworked"]))
		{
			$_ticketobj->addTimeWorked($_POST["opt_timeworked"]);
		}
	}

	header("location: index.php?_m=tickets&_a=manage");
	exit;



/**
* ###############################################
* DELETE TICKET NOTE
* ###############################################
*/
} else if ($_GET["action"] == "delticketnote") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if (!empty($_GET["ticketnoteid"]))
	{
		$_ticketobj->deleteTicketNote($_GET["ticketnoteid"]);
	}

	printRedirect(sprintf($_SWIFT["language"]["notedconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&notedconfirm=1");
	exit;


/*###################################
* Custom Field delete (Mahesh Slaria)
* ###################################*/
}else if ($_GET["action"] == "delcflink") {
	require_once ("./includes/functions_customfields.php");
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}
	if (!empty($_GET["customfieldlinkid"]))
	{
		deleteTicketCustomFieldValue(CUSTOMFIELD_USERTICKET, $_GET["customfieldlinkid"]);
		deleteTicketCustomFieldValue(CUSTOMFIELD_STAFFTICKET, $_GET["customfieldlinkid"]);
		deleteTicketCustomFieldValue(CUSTOMFIELD_STAFFUSERTICKET, $_GET["customfieldlinkid"]);
		deleteTicketCustomFieldValue(CUSTOMFIELD_USER, $_GET["customfieldlinkid"]);
	}
	printRedirect(sprintf($_SWIFT["language"]["cfdelconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&cfdelconfirm=1");
	exit;


/**
* ###############################################
* GENERAL TAB
* ###############################################
*/
} else if ($_POST["action"] == "generaltab") {
	require_once ("./modules/tickets/functions_ticketcore.php");
	$_ticketobj =& new TicketMain($_POST["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_olddepartmentid = $_ticketobj->ticket["departmentid"];
	$_oldticketstatusid = $_ticketobj->ticket["ticketstatusid"];

	$ticketlistmove = false;
	
	$_duecaltime = date("d/m/Y", $_ticketobj->ticket["duetime"]);
	// Do we need to update the due timeline?
	if (!empty($_POST["opt_due"]) && $_POST["opt_due"] != $_duecaltime)
	{
		$_ticketobj->markAsDue(getCalendarDateline($_POST["opt_due"]));
	} else if (empty($_POST["opt_due"]) && !empty($_ticketobj->ticket["duetime"])) {
		$_ticketobj->clearDue();
	}

	if ($_POST["tt_staffid"] != $_ticketobj->ticket["ownerstaffid"])
	{
		$_ticketobj->assignTicket($_POST["tt_staffid"]);
	}

	//echo "Status 0:" . $_POST["tt_ticketstatusid"];
	if ($_POST["tt_ticketstatusid"] != $_ticketobj->ticket["ticketstatusid"])
	{
		//echo "Status 1:" . $_POST["tt_ticketstatusid"];
		$_ticketobj->changeStatus($_POST["tt_ticketstatusid"]);
	}
	if ($_POST["tt_priorityid"] != $_ticketobj->ticket["priorityid"])
	{
		$_ticketobj->changePriority($_POST["tt_priorityid"]);
	}

	//echo "Department ID:" . $_POST["tt_departmentid"];
	if ($_POST["tt_departmentid"] != $_ticketobj->ticket["departmentid"])
	{
		// Get the assigned departments
		$_ticketobj->moveTicket($_POST["tt_departmentid"]);
		$_assigns = getAssignedDepartments(MODULE_TICKETS);
		if (!in_array($_POST["tt_departmentid"], $_assigns))
		{
			// Move to Ticket List
			$ticketlistmove = true;
		}
	}
	//exit;
	$_ticketobj->loadTicketLabels();
	$_ticketlabelidlist = array();
	foreach ($_ticketobj->ticket["labels"] as $key=>$val)
	{
		$_ticketlabelidlist[] = $val["ticketlabelid"];
	}

	if ((count($_ticketlabelidlist) && count($_POST["opt_ticketlabelid"])) || (!count($_ticketlabelidlist) && count($_POST["opt_ticketlabelid"])))
	{
		foreach ($_SWIFT["ticketlabelcache"] as $key=>$val)
		{
			// If in ticket list but not in post list then its been removed...
			if (in_array($val["ticketlabelid"], $_ticketlabelidlist) && !in_array($val["ticketlabelid"], $_POST["opt_ticketlabelid"]))
			{
				$_ticketobj->deleteTicketLabelLink($val["ticketlabelid"]);

			// If not in list but in post list then its been added...
			} else if (!in_array($val["ticketlabelid"], $_ticketlabelidlist) && in_array($val["ticketlabelid"], $_POST["opt_ticketlabelid"])) {
				$_ticketobj->insertTicketLabelLink($val["ticketlabelid"]);
			}
		}
	}

	// All labels emptied
	if (!count($_POST["opt_ticketlabelid"]) && count($_ticketlabelidlist))
	{
		$_ticketobj->deleteAllTicketLabels();
	}

	if (!empty($_POST["ticketlabelid"])) {
		header("location: index.php?_m=tickets&_a=manage&ticketlabelid=".intval($_POST["ticketlabelid"]));
		exit;
	} else {
		printRedirect(sprintf($_SWIFT["language"]["tcconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_olddepartmentid) ."&ticketstatusid=". intval($_oldticketstatusid));
		exit;
	}

/*	if ($ticketlistmove == true)
	{
		printRedirect(sprintf($_SWIFT["language"]["tcconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=manage&departmentid=". intval($_ticketobj->ticket["departmentid"]) ."&ticketstatusid=". intval($_ticketobj->ticket["ticketstatusid"]) ."&tcconfirm=".urlencode($_ticketobj->ticket["fticketid"]));
	} else {
		printRedirect(sprintf($_SWIFT["language"]["tcconfirm"], htmlspecialchars($_ticketobj->ticket["fticketid"])), "index.php?_m=tickets&_a=viewticket&ticketid=". intval($_ticketobj->ticket["ticketid"]) ."&tcconfirm=1");
	}*/



/**
* ###############################################
* DELETE RECIPIENTS
* ###############################################
*/
} else if ($_GET["action"] == "delrecipient" && !empty($_GET["ticketid"]) && !empty($_GET["recipientid"]) && $_SWIFT["staff"]["tdelrecipients"] != "0") {
	require_once ("./modules/tickets/functions_ticketcore.php");
	$_ticketobj =& new TicketMain($_GET["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_ticketobj->deleteRecipient($_GET["recipientid"]);

	header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]));
	exit;



/**
* ###############################################
* DELETE BILLING ENTRIES
* ###############################################
*/
} else if ($_GET["action"] == "delbillentry" && !empty($_GET["ticketid"]) && !empty($_GET["timetrackid"])) {
	$_ticketobj =& new TicketMain($_GET["ticketid"], true);

	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	$_ticketobj->deleteBillingEntry($_GET["timetrackid"]);

	header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]));
	exit;



/**
* ###############################################
* NEW LABEL
* ###############################################
*/
} else if ($_POST["action"] == "newlabel") {
	require_once ("./modules/tickets/functions_labels.php");

	if (!empty($_POST["title"]))
	{
		insertTicketLabel($_POST["title"], SWIFTPUBLIC);
	}

	header("location: index.php?_m=tickets&_a=manage&departmentid=". intval($_POST["departmentid"]) ."&ticketstatusid=". intval($_POST["ticketstatusid"]));
}






?>